package com.doucome.snapshot.view
{
	import com.adobe.serialization.json.JSON;
	import com.doucome.snapshot.Global;
	import com.doucome.snapshot.User;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	/**
	 * ...
	 * @author wkl
	 */
	public class BG extends MovieClip
	{
		private var _info_xz:TextField;
		private var _info_fen:TextField;
		private var _info_times:TextField;
		private var _info_likes:TextField;
		private var _info_bb:TextField;
		private var _info_nickname:TextField;
		private var _hedpic_loader:Loader;
		
		private var _xz:int = 0;
		private var _fen:int = 0;
		private var _times:int = 0;
		private var _likes:int = 0;
		private var _bb:int = 0;
		private var _user:User;
		
		private var _ld:URLLoader;
		
		public static const SYNC_COMPLETE_EVENT:String = "sync_conmplete";
		
		public function BG(user:User)
		{
			_user = user;
			init();
		}
		
		private function init()
		{
			
			var stylesheet:StyleSheet = new StyleSheet();
			stylesheet.parseCSS(".info {font-size: 14px;font-family: Verdana, Arial, Helv, Helvetica, sans-serif;vertical-align: super; } ");
			stylesheet.parseCSS(".info_white {font-size: 14px;font-family: Verdana, Arial, Helv, Helvetica, sans-serif;vertical-align: super; color:#ffffff;} ");
			
			_info_xz = new TextField();
			_info_xz.styleSheet = stylesheet;
			_info_xz.autoSize = "left";
			_info_xz.selectable = false;
			_info_xz.x = 80;
			_info_xz.y = 88;
			_info_xz.htmlText = "<p><span class='info'>" + xz + "</span></p>";
			
			_info_fen = new TextField();
			_info_fen.styleSheet = stylesheet;
			_info_fen.autoSize = "left";
			_info_fen.selectable = false;
			_info_fen.x = 230;
			_info_fen.y = 88;
			_info_fen.htmlText = "<p><span class='info'>" + _fen + "</span></p>";
			
			_info_times = new TextField();
			_info_times.styleSheet = stylesheet;
			_info_times.autoSize = "left";
			_info_times.selectable = false;
			_info_times.x = 395;
			_info_times.y = 70;
			_info_times.htmlText = "<p><span class='info'>" + _times + "</span></p>";
			
			_info_likes = new TextField();
			_info_likes.styleSheet = stylesheet;
			_info_likes.autoSize = "left";
			_info_likes.selectable = false;
			_info_likes.x = 538;
			_info_likes.y = 88;
			_info_likes.htmlText = "<p><span class='info'>" + _likes + "</span></p>";
			
			//_info_bb = new TextField();
			//_info_bb.styleSheet = stylesheet;
			//_info_bb.autoSize = "left";
			//_info_bb.selectable = false;
			//_info_bb.x = 672;
			//_info_bb.y = 88;
			//_info_bb.htmlText = "<p><span class='info'>" + _bb + "</span></p>";
			
			_info_nickname = new TextField();
			_info_nickname.styleSheet = stylesheet;
			_info_nickname.autoSize = "left";
			_info_nickname.selectable = false;
			_info_nickname.x = 640;
			_info_nickname.y = 4;
			_info_nickname.htmlText = "<p><span class='info_white'>" + _user.nickname + "</span></p>";
			
			addChild(_info_xz);
			addChild(_info_fen);
			addChild(_info_times);
			addChild(_info_likes);
			//addChild(_info_bb);
			addChild(_info_nickname);
			
			//
			_hedpic_loader = new Loader();
			_hedpic_loader.x = 600;
			_hedpic_loader.y = 4;
			var figurl = _user.figureurl; //"http://thirdapp0.qlogo.cn/qzopenapp/d8219673598dbd6f7bab6efcc1705ca216b06f8c6f8dea7a9875285503236f39/30";
			try
			{
				_hedpic_loader.load(new URLRequest(figurl));
			}
			catch (e:Error)
			{
				//trace(e);
			}
			
			addChild(_hedpic_loader);
		
		}
		
		public function sync():void
		{
			//同步用户数据
			var rq:URLRequest = new URLRequest("http://" + Global.APP_SERVER + "/snapshot/index.php");
			var vars:URLVariables = new URLVariables("a=sync&userid=" + _user.id);
			rq.data = vars;
			_ld = new URLLoader();
			_ld.addEventListener(Event.OPEN, onSyncStart);
			_ld.addEventListener(Event.COMPLETE, onSyncComplete);
			_ld.addEventListener(IOErrorEvent.IO_ERROR, onSyncError);
			_ld.load(rq);
		}
		
		private function onSyncError(e:IOErrorEvent):void
		{
			//_waitEffect.close();
			//_window_error.open(Local.ERROR_TXT);
		}
		
		private function onSyncComplete(e:Event):void
		{
			
			_ld.removeEventListener(Event.OPEN, onSyncStart);
			_ld.removeEventListener(Event.COMPLETE, onSyncComplete);
			_ld.removeEventListener(IOErrorEvent.IO_ERROR, onSyncError);
			
			//trace("sync complete");
			
			if (null !== e.target.data)
			{
				
				var sync_info:Array = com.adobe.serialization.json.JSON.decode(e.target.data);
				xz = sync_info[0].xz;
				times = sync_info[0].times;
				fen = sync_info[0].fen;
				likes = sync_info[0].likes;
			}
			else
			{
				//_window_error.open(Local.ERROR_TXT);
			}
			
			dispatchEvent(new Event(SYNC_COMPLETE_EVENT));
		
		}
		
		private function onSyncStart(e:Event):void
		{
		
		}
		
		public function get info_xz():TextField
		{
			return _info_xz;
		}
		
		public function set info_xz(value:TextField):void
		{
			_info_xz = value;
		}
		
		public function get info_fen():TextField
		{
			return _info_fen;
		}
		
		public function set info_fen(value:TextField):void
		{
			_info_fen = value;
		}
		
		public function get info_times():TextField
		{
			return _info_times;
		}
		
		public function set info_times(value:TextField):void
		{
			_info_times = value;
		}
		
		public function get info_likes():TextField
		{
			return _info_likes;
		}
		
		public function set info_likes(value:TextField):void
		{
			_info_likes = value;
		}
		
		public function get info_bb():TextField
		{
			return _info_bb;
		}
		
		public function set info_bb(value:TextField):void
		{
			_info_bb = value;
		}
		
		public function get xz():int
		{
			return _xz;
		}
		
		public function set xz(value:int):void
		{
			_xz = value;
			_info_xz.htmlText = "<p><span class='info'>" + value + "</span></p>";
		}
		
		public function get fen():int
		{
			return _fen;
		}
		
		public function set fen(value:int):void
		{
			_fen = value;
			_info_fen.htmlText = "<p><span class='info'>" + value + "</span></p>";
		}
		
		public function get times():int
		{
			return _times;
		}
		
		public function set times(value:int):void
		{
			_times = value;
			_info_times.htmlText = "<p><span class='info'>" + value + "</span></p>";
		}
		
		public function get likes():int
		{
			return _likes;
		}
		
		public function set likes(value:int):void
		{
			_likes = value;
			_info_likes.htmlText = "<p><span class='info'>" + value + "</span></p>";
		}
		
		public function get bb():int
		{
			return _bb;
		}
		
		public function set bb(value:int):void
		{
			_bb = value;
			_info_bb.htmlText = "<p><span class='info'>" + value + "</span></p>";
		}
	}

}

